package com.joey.cloud.auth.controller;

import com.joey.cloud.annotation.auth.JoeHadAuth;
import com.joey.cloud.auth.form.PwdLoginForm;
import com.joey.cloud.auth.form.PwdRegisterForm;
import com.joey.cloud.auth.service.AuthPwdService;
import com.joey.cloud.communal.utils.JwtUtils;
import com.joey.cloud.communal.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author joey
 */
@Api(tags = "授权-密码模式")
@ApiSort(value = 1)
@RestController
@Slf4j
public class AuthPwdController {
    @Resource
    AuthPwdService authPwdService;

    @ApiOperation(value = "注册")
    @PostMapping("/pwd/register")
    public ResponseUtil pwdRegister(@Validated PwdRegisterForm form){
        try {
            return authPwdService.pwdRegister(form);
        }catch (Exception e){
            log.error("密码模式注册出错",e);
            return ResponseUtil.error();
        }
    }

    @ApiOperation(value = "登陆")
    @PostMapping("/pwd/login")
    public ResponseUtil pwdLogin(@Validated PwdLoginForm form){
        try {
            return authPwdService.pwdLogin(form);
        }catch (Exception e){
            log.error("密码模式-登陆出错",e);
            return ResponseUtil.error();
        }
    }
    @ApiOperation(value = "获取用户信息")
    @GetMapping("/queryUserInfo")
    @JoeHadAuth()
    public ResponseUtil queryUserInfo(){
        try {
            return authPwdService.queryUserInfo();
        }catch (Exception e){
            log.error("获取用户信息出错",e);
            return ResponseUtil.error();
        }
    }

    @ApiOperation(value = "刷新token")
    @PostMapping("/refreshToken")
    public ResponseUtil refreshToken(){
        try {
            return ResponseUtil.success(JwtUtils.refreshToken());
        }catch (Exception e){
            log.error("刷新token出错",e);
            return ResponseUtil.success();
        }
    }
}
